package com.tedu.food.sys.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.tedu.food.common.util.ShiroUtils;
import com.tedu.food.common.vo.JsonResult;
import com.tedu.food.sys.entity.Member;
import com.tedu.food.sys.service.MemberService;

@Controller
@RequestMapping("/")
public class Membercontroller {
	@RequestMapping("/index")
    public String index(){
        return "index";
    }
    @RequestMapping("/register")
    public String register(){
        return "register";
    }
    @RequestMapping("/userAgreement")
    public String userAgreement(){
    	return "userAgreement";
    }

    @RequestMapping("/shop") 
    public String starter(){ 
    	return "shop"; 
    }
	 @RequestMapping("/login2") 
	 public String login2(){ 
		 return "login2"; 
	}
	 @RequestMapping("/ChangePassword") 
	 public String ChangePassword(){ 
		 return "ChangePassword"; 
	 }
	 
    @RequestMapping("/doLogin")
	   @ResponseBody
	   public JsonResult doLogin(String username,String password){
		   //1.获取Subject对象
		   Subject subject=SecurityUtils.getSubject();
		   //2.通过Subject提交用户信息,交给shiro框架进行认证操作
		   //2.1对用户进行封装
		   UsernamePasswordToken token=
		   new UsernamePasswordToken(
				   username,//身份信息
				   password);//凭证信息
		   //2.2对用户信息进行身份认证
		   subject.login(token);
		   //分析:
		   //1)token会传给shiro的SecurityManager
		   //2)SecurityManager将token传递给认证管理器
		   //3)认证管理器会将token传递给realm
		   return new JsonResult("login ok");
	   }
    @RequestMapping("/login3")
    public String login3(){
    	return "login3";
    }
    @RequestMapping("/register1")
    public String register1(){
        return "register1";
    }
    @Autowired
    private MemberService memberService;
    @RequestMapping("doSaveObject")
	@ResponseBody
	public JsonResult doSaveObject(
			Member entity,
			Integer[] roleIds){
    	memberService.saveObject(entity,roleIds);
		return new JsonResult("注册成功，请登录...");
	}
    @RequestMapping("dovalidateUsername")
    @ResponseBody
    public JsonResult dovalidateUsername(String username) {
    	memberService.findMemberByUsername(username);
    	return new JsonResult("该用户名可用");
    }
    @RequestMapping("dovalidatePhone")
    @ResponseBody
    public JsonResult dovalidatePhone(String phone) {
    	memberService.findMemberByPhone(phone);
    	return new JsonResult("该手机号可用");
    }
    @GetMapping("doFindUser")
    @ResponseBody
    public JsonResult doFindUser() {
    	return new JsonResult(ShiroUtils.getUser());
    }
}
